package com.renjun.smweb.controller.RoleController;

import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.annotation.AuthorityDeclaration;
import com.renjun.smweb.commons.util.ResponseUtils;
import com.renjun.smweb.service.RoleService.RoleService;
import com.renjun.smweb.annotation.AuthorityDeclaration;
import com.renjun.smweb.commons.util.ResponseUtils;
import com.renjun.smweb.service.RoleService.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.Map;

/**
 * Created by huamin on 2016/7/20.
 * 角色管理M2
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 角色列表M2-1
     *
     * @param {limit:10,offset:0,order:"asc",sort:"rolename"}
     * @return [{"id":"2","rolename":"市管理员","roletype":"1","roledesc":"市管理员","enabled":"1"}]
     * by huamin
     */
    @RequestMapping(value = "/queryRoleList", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M2-1", desc = "查询角色列表",reqParam = "limit,offset,order,sort",resBody = "id,rolename,roletype,roledesc,enabled")
    public JSONObject queryRoleList(HttpServletRequest request, HttpServletResponse response) {
        // TODO:  M2-1查询角色列表
        Map<String, Object> map = ResponseUtils.createRequestAndSessionLoginInfo(request);
        map.remove("districtcode");
        JSONObject rs = null;
        try {
            rs = roleService.queryRoleList(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 新增一个角色M2-2
     *
     * @param {rolename:"",roletype:"",roledesc:"",districtcode:""}
     * @return
     * by huamin
     */
    @RequestMapping(value = "/addNewRole", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M2-2", desc = "新增一个角色",reqParam = "rolename,roletype,roledesc,districtcode",resBody = "添加成功",errMsgs = "sql异常")
    public JSONObject addNewRole(HttpServletRequest request, HttpServletResponse response) {
        // TODO:  M2-2新增一个角色
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        try {
            rs = roleService.addNewRole(params);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * 修改角色信息M2-3
     *
     * @param {roleid:"",rolename:"",roletype:"",roledesc:""}
     * @return
     * by huamin
     */
    @RequestMapping(value = "/updateRole", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M2-3", desc = "修改角色信息",reqParam = "roleid,rolename,roletype,roledesc",resBody = "修改成功")
    public JSONObject updateRole(HttpServletRequest request, HttpServletResponse response) {
        // TODO:  M2-3修改角色信息
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
        try {
            rs = roleService.updateRole(params);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

    /**
     * M2-4恢复或注销角色信息
     *
     * @param {roleid:"",enabled:""(1恢复0注销)}
     * @return
     * by huamin
     */
    @RequestMapping(value = "/recoverOrCancelRole", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M2-4", desc = "恢复或注销角色信息",reqParam = "roleid,enabled(1恢复0注销)",resBody = "修改成功")
    public JSONObject recoverOrCancelRole(HttpServletRequest request, HttpServletResponse response) {
        // TODO:  M2-4恢复或注销角色信息（暂不需要）
        Map<String, Object> params = ResponseUtils.createRequestAndSessionLoginInfo(request);
        JSONObject rs = ResponseUtils.createErrorResponseBody("该接口暂时不开放");
//        JSONObject rs = ResponseUtils.createErrorResponseBody(null);
//        try {
//            rs = roleService.recoverOrCancelRole(params);
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return ResponseUtils.createErrorResponseBody(e.getMessage());
//        }
        return rs;
    }

    /**
     * 角色查看M2-5
     *
     * @param {roleid:"2"}
     * @return [{"id":"2","rolename":"市管理员","roletype":"1","roledesc":"市管理员","enabled":"1"}]
     * by huamin
     */
    @RequestMapping(value = "/queryRoleById", method = RequestMethod.POST)
    @ResponseBody
    @AuthorityDeclaration(id = "M2-5", desc = "角色查看",reqParam = "roleid",resBody = "funclist,menulist,dataareas",errMsgs = "sql异常,找不到对应数据")
    public JSONObject queryRoleById(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = ResponseUtils.createRequestAndSessionLoginInfo(request);
        // TODO:  M2-5角色查看@param params｛用户ID：roleid｝
        JSONObject rs = null;
        try {
            rs = roleService.queryRoleById(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtils.createErrorResponseBody(e.getMessage());
        }
        return rs;
    }

}
